package com.taptech.doufu.ui.view.topicview;

import android.content.Context;
import android.content.Intent;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.ui.activity.BrowseActivity;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.constant.UmengEventName;
import com.taptech.doufu.bean.novel.VipMixWorksBean;
import com.taptech.doufu.bean.novel.VipTopicBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.event.EventBusChangeVipState;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.ui.view.topicview.adapter.VipPrivitegeHorizontalAdapter;
import com.taptech.doufu.util.TMAnalysis;
import com.taptech.doufu.weex.notification.QLXNotificationCenter;
import com.taptech.doufu.weex.notification.QLXNotificationListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/4/13/013.
 */
public class VipPrivitegeView extends LinearLayout{
    private VipPrivitegeHorizontalAdapter adapter;
    private TextView tvTitle;
    private View llMore;
    private String title;
    private List<VipMixWorksBean> list;
    private Context context;
    public VipPrivitegeView(Context context) {
        super(context);
    }

    public VipPrivitegeView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public VipPrivitegeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private View root_layout;
    private View linearLayout0;
    private TextView tv_more,tvMoreTitle;
    private ImageView iv_more;
    private RecyclerView listView;
    private LinearLayout llPayVip;
    private VipTopicBean model;


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onChangeVipState(EventBusChangeVipState event) {
        if (event != null && EventBusChangeVipState.TAG_CHANGE_VIPSTATE.equals(event.getTag())) {
            getVipState();
        }
    }


    public VipPrivitegeView(final Context context, final VipTopicBean model){
        this(context);
        EventBus.getDefault().register(this);
        View view = LayoutInflater.from(context).inflate(R.layout.vip_privitege_view, null);
        this.context = context;
        this.model = model;
        linearLayout0 = view.findViewById(R.id.home_dfnovel_layout0);
        iv_more = (ImageView) view.findViewById(R.id.dfnovel_icon_more);
        tv_more = (TextView) view.findViewById(R.id.tv_more);
        tvMoreTitle = (TextView) view.findViewById(R.id.tvMoreTitle);
        if (!TextUtils.isEmpty(model.getMoreTitle())) {
            tvMoreTitle.setText(model.getMoreTitle());
        }
        QLXNotificationCenter.getInstance().addNotify("TFHomeVipCardNeedUpdateNotification", this, new QLXNotificationListener() {
            @Override
            public void notify(String event, Object data) {
                getVipState();
            }
        });
        root_layout = view.findViewById(R.id.root_layout);
        tvTitle = (TextView) view.findViewById(R.id.tvTitle);
        llMore = view.findViewById(R.id.llMore);
        listView = (RecyclerView) view.findViewById(R.id.listView);
        llPayVip = (LinearLayout) view.findViewById(R.id.llPayVip);
        llPayVip.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                TMAnalysis.event(context,UmengEventName.VIP_JUMP_TO_PAY);
                if (AccountService.getInstance().isLogin()) {
                    if (!TextUtils.isEmpty(VipPrivitegeView.this.model.getMoreUrl())) {
                        if (VipPrivitegeView.this.model.getMoreUrl().contains("weex")) {
                            SimpleWeexActivity.startActivity(context,VipPrivitegeView.this.model.getMoreUrl());
                        }else{
                            Intent intent = new Intent();
                            intent.setClass(getContext(), BrowseActivity.class);
                            intent.putExtra(Constant.URL, VipPrivitegeView.this.model.getMoreUrl());
                            intent.putExtra(Constant.TITLE, VipPrivitegeView.this.model.getTitle());
                            getContext().startActivity(intent);
                        }
                    }else{
                        SimpleWeexActivity.startActivity(context,"vue/personalcenter/TFBuyMember.js");
                    }
                } else {
                    AccountService.getInstance().jumpToLogin();
                }

            }
        });
        tvTitle.setText(model.getTitle());
        title = model.getTitle();
        changeDayModelBg();
        list = model.getSimpleList();
        llMore.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                UmengEventUtil.addEvent(context,UmengEventName.VIP_CARD_RIGHT_TOP_BTN,model.getEventBean());
                SimpleWeexActivity.startActivity(context,"vue/personalcenter/TFMemberPrivilege.js");
            }
        });
        RecyclerView listView = (RecyclerView) view.findViewById(R.id.listView);
        MeasuredLinearLayoutManager manager = new MeasuredLinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        manager.setScrollEnabled(true);
        adapter = new VipPrivitegeHorizontalAdapter(context, list,model.getEventBean());
        listView.setLayoutManager(manager);
        listView.setAdapter(adapter);
        addView(view);
    }





    public void getVipState() {
        Call<ResponseBody> call =  ApiClient.getInstance().setUseCache(false).getService().getVipState();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String json = new String(response.body().bytes());
                    JSONObject jsonObject = JSON.parseObject(json);
                    JSONObject data = jsonObject.getJSONObject("data");
                    String moreUrl = data.getString("moreUrl");
                    String moreTitle = data.getString("moreTitle");
                    if (!TextUtils.isEmpty(moreUrl)) {
                        VipPrivitegeView.this.model.setMoreUrl(moreUrl);
                    }
                    if (!TextUtils.isEmpty(moreTitle)) {
                        VipPrivitegeView.this.model.setMoreTitle(moreTitle);
                        tvMoreTitle.setText(moreTitle);
                    }

                } catch (Exception e) {
                }

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
            }
        });
    }

    public void refrush(VipTopicBean model) {
        if (model == null|| !TextUtils.isEmpty(model.getTitle()) && model.getTitle().equals(title)) {
            return;
        }
        title = model.getTitle();
        list = model.getSimpleList();
        if (llMore != null) {
            if(!TextUtils.isEmpty(model.getUrl())){
                llMore.setVisibility(VISIBLE);
            }else{
                llMore.setVisibility(GONE);
            }
        }
        if (tvTitle != null) {
            tvTitle.setText(model.getTitle());
        }
        if (adapter != null) {
            adapter.refrush(list);
        }
    }

    public void changeDayModelBg(){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            darkBg();
        }else {
            dayBg();
        }
    }




    private void dayBg() {
        root_layout.setBackgroundColor(context.getResources().getColor(R.color.home_list_item_color));
        linearLayout0.setBackgroundColor(context.getResources().getColor(R.color.white));
        listView.setBackgroundColor(context.getResources().getColor(R.color.white));
        tvTitle.setTextColor(context.getResources().getColor(R.color.text_color_8));
        tv_more.setTextColor(context.getResources().getColor(R.color.text_color_7));
        tv_more.setTextColor(context.getResources().getColor(R.color.text_color_7));
        iv_more.setAlpha(1.0f);
    }

    private void darkBg() {
        root_layout.setBackgroundColor(context.getResources().getColor(R.color.bg_dark));
        linearLayout0.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));
        listView.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));
        tvTitle.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        tv_more.setTextColor(context.getResources().getColor(R.color.text_dark));
        tv_more.setTextColor(context.getResources().getColor(R.color.text_dark));
        iv_more.setAlpha(1.0f);
    }
}
